﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Estimate.Common.DataFileFinder
{
    /// <summary>
    /// 排班表文件发现类
    /// 返回的是一个目录中最新的文件列表
    /// </summary>
    public class ScheduleFinder : IDataFileFinder
    {

        public string[] GetLatestFiles(string[] pathArray)
        {//根据排斑表的命名规范，获取最新的数据文件

            //每种类型的文件，在字典中只存在一份拷贝
            Dictionary<string, string> onlyFiles = new Dictionary<string, string>();
            
            foreach (string path in pathArray)
            {
                string filename = Path.GetFileName(path);
                string prefix = filename.Substring(0, 15);
                string number = filename.Substring(15, 2);
                if (onlyFiles.ContainsKey(prefix))
                {//如果已经存在，则比较其number,转化为int后更大的更新进字典
                    string existNumber = onlyFiles[prefix];
                    if (Convert.ToInt32(existNumber) < Convert.ToInt32(number))
                    {
                        onlyFiles[prefix] = number;
                    }
                }
                else
                {//不存在，则直接进入字典 
                    onlyFiles[prefix] = number;
                }
            }

            List<string> result = new List<string>( onlyFiles.Count);
            foreach (KeyValuePair<string, string> pair in onlyFiles)
            {
                result.Add(string.Format("{0}{1}.pbb", pair.Key, pair.Value));
            }
            return result.ToArray();
        }

        public string[] GetFileFromDirectory(DateTime dt, string dirPath)
        {
            List<string> files = new List<string>(26);
            if (Directory.Exists(dirPath))
            {
                files.AddRange(Directory.GetFiles(dirPath, "cz_?????_" + dt.ToString("yyyyMM") + "??.pbb")); 
            }
            else
            {
                Console.WriteLine("Can't find directory named {0}", dirPath);
            }

            if (Directory.Exists(dirPath))
            {
                files.AddRange(Directory.GetFiles(dirPath, "zd_00001_" + dt.ToString("yyyyMM") + "??.pbb"));
            }
            else
            {
                Console.WriteLine("Can't find directory named {0}", dirPath);
            }
            

            string[] result = GetLatestFiles(files.ToArray());
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = string.Format("{0}{1}{2}", new object[]
                                {
                                    Path.GetDirectoryName( dirPath),
                                    Path.DirectorySeparatorChar,
                                    result[i]
                                }
                                );
            }
            return result;
        }


        public string[] GetFileNames(DateTime dt)
        {
            throw new NotImplementedException();
        }


        public Dictionary<string, string[]> GetFileListFromDirectory(DateTime dt, string dirPath)
        {
            throw new NotImplementedException();
        }
    }
}
